#### "How do past repression and indoctrination affect redistributive preferences?" ####
# authors: "Pelke, Lars"
# date: 2019-10-23
# written under "R version 3.6.0 (2019-03-11)"

#### Preliminaries ####

R.version$version.string

# clear workspace
rm(list=ls())

# set working directory

#functions 
mean.new <- function(v) {
  if (all(is.na(v))) { return(NA) } else { return(mean(v, na.rm=T)) }
}



#### Load data ####

vdem_data <- readRDS("data/vdem_data.rds") 
vdem_cohort <- readRDS("data/vdem_cohort.rds") 
merged_data <- readRDS("data/merged_data.rds") 

#### Rename Variables ####

merged_data <- merged_data %>%
  rename(cowcode = cown) #

vdem_data <- vdem_data %>%
  dplyr::select(-cowcode)

#### Merging Data with period effect and cohort effects ####

merged_data <- merged_data %>%
  drop_na(iso3n, year)

merged_data <- merged_data %>%
  left_join(vdem_data, c("iso3n", "year")) 

merged_data <- merged_data %>%
  left_join(vdem_cohort, c("country_id", "cohortmatch5_15"))

merged_data <- merged_data %>%
  drop_na(iso3n, cohortmatch5_15)

#### Construct Variable Time Under Autocracy / Time under Democracy ####

merged_data <- merged_data %>%
  mutate(birth_year = year - age, # construct birth_year
         birth_year = birth_year -1900) %>%  # birth_year minus 1900,  time under autocracy only after 1900
  mutate(birth_year = ifelse(birth_year<0, NA, birth_year)) %>%
  drop_na(birth_year)

overview <- merged_data %>%
  group_by(data, wave, iso3n) %>%
  summarize(mean_government_resp = mean.new(year))


summary(merged_data$birth_year)

merged_data <- merged_data %>%
  mutate(time_under_autocracy = autocracy_years - birth_year,  # years that an individual lived under autocracy
         time_under_autocracy = ifelse(time_under_autocracy<0, 0, time_under_autocracy ), # correct minus values to 0, because that are democratic years
         time_under_democracy = democracy_years - birth_year,  # years that an individual lived under democracy
         time_under_democracy = ifelse(time_under_democracy<0, 0, time_under_democracy))

summary(merged_data$time_under_autocracy)
summary(merged_data$time_under_democracy) 

#### Birth Year plus 15 years to count only after years under autocracy after 15 ####

merged_data <- merged_data %>%
  mutate(year_15 = birth_year + 15)

summary(merged_data$year_15)
summary(merged_data$autocracy_years)

merged_data <- merged_data %>%
  mutate(time_under_autocracy_15 = autocracy_years - year_15,  # years that an individual lived under autocracy
         time_under_autocracy_15 = ifelse(time_under_autocracy_15<0, 0, time_under_autocracy_15 ), # correct minus values to 0, because that are democratic years
         time_under_democracy_15 = democracy_years - year_15,  # years that an individual lived under democracy
         time_under_democracy_15 = ifelse(time_under_democracy_15<0, 0, time_under_democracy_15))

summary(merged_data$time_under_autocracy_15)
summary(merged_data$time_under_democracy_15) 


saveRDS(merged_data, file = "data/merged_data_master.rds")




